/**
 * Created by
 */
var Rest=function () {
    var exports = {};
    /**
     * 统一的ajax 提交方法,提供自定义回调函数
     * @param restUrl 访问地址
     * @param httpMethod  POST GET 等
     * @param param 参数
     * @param contenttype 'application/json';"application/x-www-form-urlencoded";
     * @param datatype json
     * @param callback 回调函数
     */
    exports.rest=function(restUrl, httpMethod, param, contenttype, datatype, async,callback) {
        var request=$.ajax({url:restUrl,type:httpMethod,data:param,contentType:contenttype,async: async,dataType:datatype});
        request.done(function (data) {
            try {
                if (data === null || data === undefined) {

                } else {
                    callback(data);
                }
            }catch (e){
                exports.errMode(e);
            }
        })

        request.fail(function(textStatus, errorThrown) {
            top.layer.alert("系统异常-fail"+textStatus+":"+errorThrown,{
                icon: 2,
                title: false,
                btn:false
            });
        })
    }
    exports.getContTypeJson=function (restUrl, param,async,callback) {
        exports.get(restUrl,param,'application/json','json',async,callback);
    }
    exports.get=function (restUrl, param, contenttype, datatype, async,callback) {
        exports.rest(restUrl,'GET',param,contenttype,datatype,async,callback);
    }

    exports.post=function (restUrl, param, contenttype,async,callback) {
        exports.rest(restUrl,"POST",param,contenttype,'json',async,callback);
    }

    /**
     * 没有回调，统一处理回调信息
     * 回调函数格式要求
     * {
     *  "ret":true,
     *  "msg":"请求成功"
     * }
     * @param restUrl
     * @param param
     * @param contenttype
     * @param datatype
     * @param async
     * @param callback
     */
    exports.getTypeJsonNotCallback=function (restUrl, param,async,index) {
        exports.rest(restUrl,"GET",param,"application/json","json",async,function (data) {
            if (data.ret){
                top.layer.msg(data.msg, {
                    icon: 1,
                    time: 2500 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    parent.layer.close(index); //再执行关闭
                });
            }else {
                top.top.layer.alert(data.msg,{
                    icon: 2,
                    title: false,
                    btn:false
                });
            }
        });
    }
    /**
     * 没有回调，统一处理回调信息
     * 回调函数格式要求
     * {
     *  "ret":true,
     *  "msg":"请求成功"
     * }
     * @param restUrl
     * @param param
     * @param contenttype
     * @param datatype
     * @param async
     * @param callback
     */
    exports.postTypeJsonNotCallback=function (restUrl, param, async,callback) {
        exports.rest(restUrl,'POST',param,'application/json','json',async,function (data) {
            if (data.ret){
                top.layer.msg(data.msg, {
                    icon: 1,
                    time: 2500 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                });
            }else {
                top.layer.alert(data.msg,{
                    icon: 2,
                    title: false,
                    btn:false
                });
            }
        });
    }
    /**
     * 没有回调，统一处理回调信息
     * 回调函数格式要求
     * {
     *  "ret":true,
     *  "msg":"请求成功"
     * }
     * @param restUrl
     * @param param
     * @param contenttype
     * @param datatype
     * @param async
     * @param callback
     * @param index  
     */
    exports.postTypeUrlencNotCallback=function (restUrl, param, async,index) {
        exports.rest(restUrl,'POST',param,'application/x-www-form-urlencoded','json',async,function (data) {
            if (data.ret){
                top.layer.msg(data.msg, {
                    icon: 1,
                    time: 1500 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    /*var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭*/
                    //window.parent.location.reload();
                    parent.layer.close(index);
                });
            }else {
                top.layer.alert(data.msg,{
                    icon: 2,
                    title: false,
                    btn:false
                });
            }
        });
    }

    exports.errMode=function (e) {
        alert(e)
        top.layer.alert("系统异常-errMode"+e,{
            icon: 2,
            title: false,
            btn:false
        });
    }
    return exports;
}

